package com.the9tcat.everydayapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.the9tcat.everydayapp.config.AppConfig;
import com.the9tcat.everydayapp.net.AppParams;
import com.the9tcat.everydayapp.net.SyncDailyApp;

public class AppDetailActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_detail_layout);
        SharedPreferences sp = getSharedPreferences(AppConfig.CONFIG_DATA_NAME, 0);
        if (sp.getBoolean("is_first_use", true)) {
            showDialog();
        }
        
//        FetchAppInfo.getCategory();
//        FetchAppInfo.getDailyApps();
//        Log.d("***", this.getFilesDir().toString());
        new Thread(new Runnable() {
        	public void run() {
        		// 测试数据。。。
        		AppParams params = new AppParams();
        		params.age = "1,2,3,4,5";
        		params.day = "20110920";
        		params.sex = "0";
        		SyncDailyApp.syncDailyApp(AppDetailActivity.this, params);
        		params.age = "1,2,3,4,5";
        		params.day = "20110922";
        		params.sex = "0";
        		SyncDailyApp.syncDailyApp(AppDetailActivity.this, params);
        	}
        }).start();
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem setting = menu.add(R.string.menu_setting);
		setting.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Intent intent = new Intent(AppDetailActivity.this, ConfigureActivity.class);
				startActivity(intent);
				return false;
			}
		});
		MenuItem about = menu.add(R.string.menu_about);
		about.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem arg0) {
				// Goto About Activity
				return false;
			}
		});
		return super.onCreateOptionsMenu(menu);
	}

	private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.dialog_title_laert);
        builder.setMessage(R.string.dialog_text_msg);
        builder.setPositiveButton(R.string.btn_text_ikown, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SharedPreferences sp = getSharedPreferences(AppConfig.CONFIG_DATA_NAME, 0);
                SharedPreferences.Editor e = sp.edit();
                e.putBoolean("is_first_use", false);
                e.commit();
            }
        });
        builder.create().show();
    }
}